import requests
import time

class FibonacciClient:
    def __init__(self, base_url="http://localhost:8001"):
        self.base_url = base_url
    
    def get_fibonacci(self, n: int, use_cache=True) -> dict:
        """获取斐波那契数"""
        try:
            response = requests.post(
                f"{self.base_url}/fibonacci",
                json={
                    "n": n,
                    "use_cache": use_cache
                },
                timeout=10.0  # 大数计算可能需要更长时间
            )
            response.raise_for_status()
            return response.json()
        except requests.exceptions.RequestException as e:
            print(f"请求失败: {str(e)}")
            raise

def run_demo():
    client = FibonacciClient()
    
    test_cases = [10, 20, 35]
    
    print("斐波那契数列演示")
    print("=" * 40)
    for n in test_cases:
        print(f"计算 fib({n})")
        
        # 带缓存测试
        start = time.time()
        result = client.get_fibonacci(n, use_cache=True)
        elapsed = time.time() - start
        print(f"结果: {result['fibonacci_number']}")
        print(f"方法: {result['method']}")
        print(f"带缓存耗时: {elapsed:.6f}秒")
        
        # 无缓存测试
        start = time.time()
        result = client.get_fibonacci(n, use_cache=False)
        elapsed = time.time() - start
        print(f"无缓存耗时: {elapsed:.6f}秒")
        print("-" * 30)

if __name__ == "__main__":
    run_demo()